package com.hefeng.wx.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.common.core.utils.DateUtils;
import com.hefeng.wx.domain.WxDevice;
import com.hefeng.wx.domain.WxOrderDistribution;
import com.hefeng.wx.domain.WxUserOrder;
import com.hefeng.wx.domain.WxUser;
import com.hefeng.wx.mapper.WxDeviceMapper;
import com.hefeng.wx.mapper.WxDeviceOrderMapper;
import com.hefeng.wx.mapper.WxOrderDistributionMapper;
import com.hefeng.wx.mapper.WxUserMapper;
import com.hefeng.wx.service.WxUserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hefeng.common.core.utils.EmptyUtils;
import util.OrderUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 设备工单信息表;(wx_device_order)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2022-9-19
 */
@Service
public class WxUserOrderServiceImpl extends ServiceImpl<WxDeviceOrderMapper, WxUserOrder> implements WxUserOrderService {
    @Autowired
    private WxDeviceOrderMapper wxDeviceOrderMapper;
    @Autowired
    private WxDeviceMapper wxDeviceMapper;
    @Autowired
    private WxUserMapper wxUserMapper;

    @Autowired
    private WxOrderDistributionMapper wxOrderDistributionMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public WxUserOrder queryById(long id){
        return wxDeviceOrderMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param wxUserOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<WxUserOrder> getOrderPage(WxUserOrder wxUserOrder, int current, int size){
        LambdaQueryWrapper<WxUserOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxUserOrder::getUserId, wxUserOrder.getUserId());
        queryWrapper.eq(WxUserOrder::getIsDeleted,1);
        queryWrapper.orderByDesc(WxUserOrder::getCreatedTime);
        Page<WxUserOrder> pagin = new Page<>(current , size);
        pagin=wxDeviceOrderMapper.selectPage(pagin,queryWrapper);
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param wxUserOrder 实例对象
     * @return 实例对象
     */
    public Map<String,Object> insert(WxUserOrder wxUserOrder){
        Map<String,Object> map=new HashMap<>();
        if (EmptyUtils.isEmpty(wxUserOrder.getUserId())){
            map.put("make",false);
            map.put("msg","用户信息不能未空！");
            return map;
        }
        if (EmptyUtils.isEmpty(wxUserOrder.getOrderUserName())){
            map.put("make",false);
            map.put("msg","用户姓名不能未空！");
            return map;
        }
        if (EmptyUtils.isEmpty(wxUserOrder.getOrderUserMobile())){
            map.put("make",false);
            map.put("msg","用户手机号码不能未空！");
            return map;
        }
        //获取用户信息
        WxUser user=wxUserMapper.selectById(wxUserOrder.getUserId());
        //新增工单开始
        wxUserOrder.setOrderCode(OrderUtils.getOrderCode());
        wxUserOrder.setOrderTitle(user.getWxNickname()+"提交的工单");
        //默认分配用户 状态为2
        wxUserOrder.setOrderState(2);
        wxUserOrder.setIsDeleted(1);
        wxUserOrder.setCreatedTime(new Date());
        wxDeviceOrderMapper.insert(wxUserOrder);
        map.put("make",true);
        map.put("order", wxUserOrder);
        //创建工单派发信息
        WxDevice device=wxDeviceMapper.selectById(wxUserOrder.getDeviceId());
        WxOrderDistribution wxOrderDistribution =new WxOrderDistribution();
        wxOrderDistribution.setWorkId(wxUserOrder.getOrderId().toString());
        wxOrderDistribution.setDeviceCode(device.getDeviceCode());
        wxOrderDistribution.setWorkType("2");
        wxOrderDistribution.setStatus("1");
        wxOrderDistribution.setDispatcher("127");
        wxOrderDistribution.setDispatchTime(new Date());
        wxOrderDistribution.setContactDetails("18228091362");
        wxOrderDistribution.setCreatedUser("管理员");
        wxOrderDistribution.setCreatedTime(new Date());
        wxOrderDistribution.setExpectedCompletionTime(DateUtils.addDays(new Date(),3));
        wxOrderDistributionMapper.insert(wxOrderDistribution);
        //发送工单至燃气平台
        return map;
    }
    
    /** 
     * 更新数据
     *
     * @param wxUserOrder 实例对象
     * @return 实例对象
     */
    public WxUserOrder update(WxUserOrder wxUserOrder){
        //1. 根据条件动态更新

            return wxUserOrder;
    }
    
    /** 
     * 通过主键删除数据
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(long id){
        int total = wxDeviceOrderMapper.deleteById(id);
        return total > 0;
    }
}